/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd. 		   */
/*																   */
/*	NAME	  = t__sqlserver_bigint_add.c						   */
/*	NOTE	  = 												   */
/*	DATE	  = 2014/11/25 by geyh								   */
/*******************************************************************/

#include <stdio.h>
#include <string.h>
#include <CUnit/Basic.h>

#include <postgresql/config.h>
#include <postgresql/postgresql_core.h>

extern int __postgresql_parse_startup(Frbyte_Packet *p_st_data, int i_packet_len, char **pp_user, char **pp_database);

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 100;
    Frbyte_Packet *p_st_data = NULL;
    char *p_user = NULL;
    char *p_database = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data,
                 (char *) "\x00\x03\x00\x00\x75\x73\x65\x72\x00\x70\x6f\x73\x74\x67\x72\x65\x73\x00\x64\x61\x74\x61\x62\x61\x73\x65\x00\x70\x6f\x73\x74\x67\x72\x65\x73\x00\x63\x6c\x69\x65\x6e\x74\x5f\x65\x6e\x63\x6f\x64\x69\x6e\x67\x00\x55\x4e\x49\x43\x4f\x44\x45\x00\x44\x61\x74\x65\x53\x74\x79\x6c\x65\x00\x49\x53\x4f\x00\x65\x78\x74\x72\x61\x5f\x66\x6c\x6f\x61\x74\x5f\x64\x69\x67\x69\x74\x73\x00\x32\x00\x00",
                 96);

    i_ret = __postgresql_parse_startup(p_st_data, i_packet_len, &p_user, &p_database);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_user, "postgres");
    CU_ASSERT_STRING_EQUAL(p_database, "postgres");

    frfree(p_user);
    frfree(p_database);
    bytebuff_free(&p_st_data);

    return;
}


static void __case2(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 7;
    Frbyte_Packet *p_st_data = NULL;
    char *p_user = NULL;
    char *p_database = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x00\x03\x00", 3);

    i_ret = __postgresql_parse_startup(p_st_data, i_packet_len, &p_user, &p_database);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);

    return;
}

static void __case3(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 9;
    Frbyte_Packet *p_st_data = NULL;
    char *p_user = NULL;
    char *p_database = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x00\x03\x00\x00\x75", 5);

    i_ret = __postgresql_parse_startup(p_st_data, i_packet_len, &p_user, &p_database);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);

    return;
}

static void __case4(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 98;
    Frbyte_Packet *p_st_data = NULL;
    char *p_user = NULL;
    char *p_database = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data,
                 (char *) "\x00\x03\x00\x00\x75\x73\x65\x72\x00\x70\x6f\x73\x74\x67\x72\x65\x73\x00\x64\x61\x74\x61\x62\x61\x73\x65\x00\x70\x6f\x73\x74\x67\x72\x65\x73\x00\x63\x6c\x69\x65\x6e\x74\x5f\x65\x6e\x63\x6f\x64\x69\x6e\x67\x00\x55\x4e\x49\x43\x4f\x44\x45\x00\x44\x61\x74\x65\x53\x74\x79\x6c\x65\x00\x49\x53\x4f\x00\x65\x78\x74\x72\x61\x5f\x66\x6c\x6f\x61\x74\x5f\x64\x69\x67\x69\x74\x73\x00\x32",
                 94);

    i_ret = __postgresql_parse_startup(p_st_data, i_packet_len, &p_user, &p_database);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);

    return;
}

static void __case5(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 99;
    Frbyte_Packet *p_st_data = NULL;
    char *p_user = NULL;
    char *p_database = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data,
                 (char *) "\x00\x03\x00\x00\x75\x73\x65\x72\x00\x70\x6f\x73\x74\x67\x72\x65\x73\x00\x64\x61\x74\x61\x62\x61\x73\x65\x00\x70\x6f\x73\x74\x67\x72\x65\x73\x00\x63\x6c\x69\x65\x6e\x74\x5f\x65\x6e\x63\x6f\x64\x69\x6e\x67\x00\x55\x4e\x49\x43\x4f\x44\x45\x00\x44\x61\x74\x65\x53\x74\x79\x6c\x65\x00\x49\x53\x4f\x00\x65\x78\x74\x72\x61\x5f\x66\x6c\x6f\x61\x74\x5f\x64\x69\x67\x69\x74\x73\x00\x32\x00",
                 95);

    i_ret = __postgresql_parse_startup(p_st_data, i_packet_len, &p_user, &p_database);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);

    return;
}

int add_suite__postgresql_parse_startup(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __postgresql_parse_startup()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    //增加测试用例
    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
